﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KMouse
{
    public partial class Form_Log : Form
    {
        private Button button_Modbus_Send;
        private Label label_Modbus_Val;
        private TextBox textBox_Modbus_Val;
        private Label label_Modbus_Reg;
        private TextBox textBox_Modbus_Reg;
        private TextBox textBox_ComRec;
        private Timer timer_Background;
        private System.ComponentModel.IContainer components;
        private Button button_ClearRec;
        private TextBox textBox_EchoBack;
        private Label label_Rcv;

        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.button_Modbus_Send = new System.Windows.Forms.Button();
            this.label_Modbus_Val = new System.Windows.Forms.Label();
            this.textBox_Modbus_Val = new System.Windows.Forms.TextBox();
            this.label_Modbus_Reg = new System.Windows.Forms.Label();
            this.textBox_Modbus_Reg = new System.Windows.Forms.TextBox();
            this.textBox_ComRec = new System.Windows.Forms.TextBox();
            this.label_Rcv = new System.Windows.Forms.Label();
            this.timer_Background = new System.Windows.Forms.Timer(this.components);
            this.button_ClearRec = new System.Windows.Forms.Button();
            this.textBox_EchoBack = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            // 
            // button_Modbus_Send
            // 
            this.button_Modbus_Send.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button_Modbus_Send.Location = new System.Drawing.Point(414, 255);
            this.button_Modbus_Send.Name = "button_Modbus_Send";
            this.button_Modbus_Send.Size = new System.Drawing.Size(47, 22);
            this.button_Modbus_Send.TabIndex = 51;
            this.button_Modbus_Send.TabStop = false;
            this.button_Modbus_Send.Text = "Send";
            this.button_Modbus_Send.UseVisualStyleBackColor = true;
            this.button_Modbus_Send.Click += new System.EventHandler(this.button_Modbus_Send_Click);
            // 
            // label_Modbus_Val
            // 
            this.label_Modbus_Val.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.label_Modbus_Val.AutoSize = true;
            this.label_Modbus_Val.Location = new System.Drawing.Point(314, 260);
            this.label_Modbus_Val.Name = "label_Modbus_Val";
            this.label_Modbus_Val.Size = new System.Drawing.Size(59, 12);
            this.label_Modbus_Val.TabIndex = 54;
            this.label_Modbus_Val.Text = "Val(dec):";
            // 
            // textBox_Modbus_Val
            // 
            this.textBox_Modbus_Val.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_Modbus_Val.Location = new System.Drawing.Point(373, 256);
            this.textBox_Modbus_Val.Name = "textBox_Modbus_Val";
            this.textBox_Modbus_Val.Size = new System.Drawing.Size(35, 21);
            this.textBox_Modbus_Val.TabIndex = 52;
            this.textBox_Modbus_Val.TabStop = false;
            this.textBox_Modbus_Val.Text = "1234";
            // 
            // label_Modbus_Reg
            // 
            this.label_Modbus_Reg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.label_Modbus_Reg.AutoSize = true;
            this.label_Modbus_Reg.Location = new System.Drawing.Point(227, 260);
            this.label_Modbus_Reg.Name = "label_Modbus_Reg";
            this.label_Modbus_Reg.Size = new System.Drawing.Size(59, 12);
            this.label_Modbus_Reg.TabIndex = 53;
            this.label_Modbus_Reg.Text = "Reg(dec):";
            // 
            // textBox_Modbus_Reg
            // 
            this.textBox_Modbus_Reg.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_Modbus_Reg.Location = new System.Drawing.Point(288, 256);
            this.textBox_Modbus_Reg.Name = "textBox_Modbus_Reg";
            this.textBox_Modbus_Reg.Size = new System.Drawing.Size(23, 21);
            this.textBox_Modbus_Reg.TabIndex = 50;
            this.textBox_Modbus_Reg.TabStop = false;
            this.textBox_Modbus_Reg.Text = "03";
            // 
            // textBox_ComRec
            // 
            this.textBox_ComRec.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_ComRec.BackColor = System.Drawing.Color.White;
            this.textBox_ComRec.Cursor = System.Windows.Forms.Cursors.Default;
            this.textBox_ComRec.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBox_ComRec.Location = new System.Drawing.Point(11, 11);
            this.textBox_ComRec.Margin = new System.Windows.Forms.Padding(2);
            this.textBox_ComRec.MaxLength = 0;
            this.textBox_ComRec.Multiline = true;
            this.textBox_ComRec.Name = "textBox_ComRec";
            this.textBox_ComRec.ReadOnly = true;
            this.textBox_ComRec.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.textBox_ComRec.Size = new System.Drawing.Size(450, 110);
            this.textBox_ComRec.TabIndex = 47;
            this.textBox_ComRec.TabStop = false;
            // 
            // label_Rcv
            // 
            this.label_Rcv.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.label_Rcv.AutoSize = true;
            this.label_Rcv.Location = new System.Drawing.Point(226, 235);
            this.label_Rcv.Margin = new System.Windows.Forms.Padding(2, 0, 2, 0);
            this.label_Rcv.Name = "label_Rcv";
            this.label_Rcv.Size = new System.Drawing.Size(137, 12);
            this.label_Rcv.TabIndex = 48;
            this.label_Rcv.Text = "Received:000000(Bytes)";
            // 
            // timer_Background
            // 
            this.timer_Background.Enabled = true;
            this.timer_Background.Tick += new System.EventHandler(this.timer_Background_Tick);
            // 
            // button_ClearRec
            // 
            this.button_ClearRec.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.button_ClearRec.Location = new System.Drawing.Point(414, 230);
            this.button_ClearRec.Name = "button_ClearRec";
            this.button_ClearRec.Size = new System.Drawing.Size(47, 22);
            this.button_ClearRec.TabIndex = 55;
            this.button_ClearRec.TabStop = false;
            this.button_ClearRec.Text = "Clear";
            this.button_ClearRec.UseVisualStyleBackColor = true;
            this.button_ClearRec.Click += new System.EventHandler(this.button_ClearRec_Click);
            // 
            // textBox_EchoBack
            // 
            this.textBox_EchoBack.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
            | System.Windows.Forms.AnchorStyles.Left) 
            | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_EchoBack.BackColor = System.Drawing.Color.White;
            this.textBox_EchoBack.Cursor = System.Windows.Forms.Cursors.Default;
            this.textBox_EchoBack.Font = new System.Drawing.Font("Courier New", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.textBox_EchoBack.Location = new System.Drawing.Point(11, 125);
            this.textBox_EchoBack.Margin = new System.Windows.Forms.Padding(2);
            this.textBox_EchoBack.MaxLength = 0;
            this.textBox_EchoBack.Multiline = true;
            this.textBox_EchoBack.Name = "textBox_EchoBack";
            this.textBox_EchoBack.ReadOnly = true;
            this.textBox_EchoBack.ScrollBars = System.Windows.Forms.ScrollBars.Both;
            this.textBox_EchoBack.Size = new System.Drawing.Size(450, 100);
            this.textBox_EchoBack.TabIndex = 56;
            this.textBox_EchoBack.TabStop = false;
            // 
            // FormLog
            // 
            this.ClientSize = new System.Drawing.Size(465, 285);
            this.ControlBox = false;
            this.Controls.Add(this.textBox_EchoBack);
            this.Controls.Add(this.button_ClearRec);
            this.Controls.Add(this.button_Modbus_Send);
            this.Controls.Add(this.label_Modbus_Val);
            this.Controls.Add(this.textBox_Modbus_Val);
            this.Controls.Add(this.label_Modbus_Reg);
            this.Controls.Add(this.textBox_Modbus_Reg);
            this.Controls.Add(this.textBox_ComRec);
            this.Controls.Add(this.label_Rcv);
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.Name = "FormLog";
            this.ShowIcon = false;
            this.Text = "Modbus log";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        public Form_Log()
        {
            InitializeComponent();
        }

        COM com_p;
        Modbus mdbs_p;
        Queue<string> queue_key_str_p;
        Queue<string> queue_message_p;

        public void Init(COM com_s, Modbus mdbs_s, Queue<string> queue_key_str_s, Queue<string> queue_message_s)
        {
            com_p = com_s;
            mdbs_p = mdbs_s;
            queue_key_str_p = queue_key_str_s;
            queue_message_p = queue_message_s;
        }

        private void button_Modbus_Send_Click(object sender, EventArgs e)
        {
            byte Reg;
            uint Val;

            if(textBox_Modbus_Reg.Text.Length == 0)
            {
                Reg = 0;
            }
            else
            {
                Reg = Convert.ToByte(textBox_Modbus_Reg.Text);
            }
            if(textBox_Modbus_Val.Text.Length == 0)
            {
                Val = 0;
            }
            else
            {
                Val = Convert.ToUInt32(textBox_Modbus_Val.Text);
            }

            mdbs_p.Send_03((Modbus.REG)Reg, 1, Val);
        }

        private void timer_Background_Tick(object sender, EventArgs e)
        {
            if(mdbs_p.echo_en == true)          //不显示modbus通讯字节时，回显按键历史
            {
                if(queue_key_str_p.Count > 0)
                {
                    textBox_EchoBack.AppendText(queue_key_str_p.Dequeue());
                }

                if(queue_message_p.Count > 0)
                {
                    textBox_ComRec.AppendText("\r\n" + queue_message_p.Dequeue());
                }

                label_Rcv.Text = "Received:" + com_p.recv_cnt.ToString() + "(Bytes)";
            }
        }

        private void button_ClearRec_Click(object sender, EventArgs e)
        {
            textBox_EchoBack.Text = "";
            textBox_ComRec.Text = "";
            com_p.recv_cnt = 0;

            mdbs_p.success_cnt = 0;
            mdbs_p.fail_cnt = 0;
        }
    }
}
